package com.jxb.service;

import com.jxb.protobuf.Buyer;
import com.jxb.protobuf.OrderQueryGrpc;
import com.jxb.protobuf.order;
import io.grpc.stub.StreamObserver;

import java.util.ArrayList;
import java.util.List;

public class OrderQueryServiceImpl extends OrderQueryGrpc.OrderQueryImplBase {

    private static List<order> mockOrders(){
        List<order> list = new ArrayList<>();
        order.Builder builder = order.newBuilder();

        for (int i = 0; i < 10; i++) {
            list.add(builder
                    .setOrderId(i)
                    .setProductId(1000+i)
                    .setOrderTime(System.currentTimeMillis()/1000)
                    .setBuyerRemark(("remark-" + i))
                    .build());
        }

        return list;
    }

    @Override
    public void listOrders(Buyer request, StreamObserver<order> responseObserver) {
        // 持续输出到client
        for (order order : mockOrders()) {
            //向客户端多次发送数据
            responseObserver.onNext(order);
        }
        // 结束输出
        responseObserver.onCompleted();
    }
}
